home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / communication / example / TalkServerThread.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.7 KB  |  118 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18. import java.net.*;
  19. import java.util.*;
  20.  
  21. class TalkServerThread extends Thread {
  22.     public Socket socket;
  23.     public DataInputStream is;
  24.     public DataOutputStream os;
  25.     TalkServer server;
  26.     boolean DEBUG = false;
  27.  
  28.     public String toString() {
  29.         return "TalkServerThread: socket = " + socket
  30.                + "; is = " + is
  31.                + "; os = " + os;
  32.     }
  33.  
  34.     TalkServerThread(Socket socket, TalkServer server) throws IOException {
  35.         super("TalkServer");
  36.  
  37.         is = new DataInputStream(socket.getInputStream());
  38.         os = new DataOutputStream(socket.getOutputStream());
  39.  
  40.         if (is == null) {
  41.             System.err.println("TalkServerThread: Input stream seemed "
  42.                                + "to be created successfully, but it's null.");
  43.             throw new IOException();
  44.         }
  45.  
  46.         if (os == null) {
  47.             System.err.println("TalkServerThread: Output stream seemed "
  48.                                + "to be created successfully, but it's null.");
  49.             throw new IOException();
  50.         }
  51.  
  52.         this.socket = socket;
  53.         this.server = server;
  54.     }
  55.  
  56.     public void run() {
  57.         while (socket != null) {
  58.             try {
  59.                 //Read data.
  60.                 String str = is.readUTF();
  61.  
  62.                 //Pass it on.
  63.                 if (str != null) {
  64.                     server.forwardString(str, this);
  65.                 }
  66.             } catch (EOFException e) { //No more data on this socket...
  67.                 server.forwardString("SERVER SAYS other applet disconnected",
  68.                                      this);
  69.                 cleanup();
  70.                 return;
  71.             } catch (NullPointerException e) { //Socket doesn't exist...
  72.                 server.forwardString("SERVER SAYS no socket to other applet",
  73.                                      this);
  74.                 cleanup();
  75.                 return;
  76.             } catch (IOException e) { //Read problem..
  77.                 server.forwardString("SERVER SAYS socket trouble with other applet",
  78.                                      this);
  79.                 cleanup();
  80.                 return;
  81.             } catch (Exception e) { //Unknown exception. Complain and quit.
  82.                 System.err.println("Exception on is.readUTF():");
  83.                 e.printStackTrace();
  84.                 cleanup();
  85.                 return;
  86.             }
  87.         }
  88.     }
  89.  
  90.     protected void finalize() {
  91.         cleanup();
  92.     }
  93.  
  94.     void cleanup() {
  95.         try {
  96.             if (is != null) {
  97.                 is.close();
  98.                 is = null;
  99.             }
  100.         } catch (Exception e) {} //Ignore errors.
  101.  
  102.         try {
  103.             if (os != null) {
  104.                 os.close();
  105.                 os = null;
  106.             }
  107.         } catch (Exception e) {} //Ignore errors.
  108.  
  109.         try {
  110.             if (socket != null) {
  111.                 socket.close();
  112.                 socket = null;
  113.             }
  114.         } catch (Exception e) {} //Ignore errors.
  115.     }
  116.  
  117. }
  118.